This tutorial aims to introduce fundamental concepts of Docker Compose by guiding you through the development of a basic Python web application.
Using the Flask framework, the application features a hit counter in Redis, providing a practical example of how Docker Compose can be applied in web development scenarios.
The concepts demonstrated here should be understandable even if you're not familiar with Python.
This is a non-normative example that just highlights the key things you can do with Compose.
PrerequisitesMake sure you have:
Installed the latest version of Docker ComposeA basic understanding of Docker concepts and how Docker worksStep 1: Set upCreate a directory for the project:
$ mkdir composetest$ cd composetestCreate a file called app.py in your project directory and paste the following code in:
import timeimport redisfrom flask import Flaskapp = Flask(__name__)cache = redis.Redis(host='redis', port=6379)def get_hit_count():retries = 5while True:try:return cache.incr('hits')except redis.exceptions.ConnectionError as exc:if retries == 0:raise excretries -= 1time.sleep(0.5)@app.route('/')def hello():count = get_hit_count()return 'Hello World! I have been seen {} times.\n'.format(count)In this example, redis is the hostname of the redis container on theapplication's network and the default port, 6379 is used.
Note
Note the way the get_hit_count function is written. This basic retryloop attempts the request multiple times if the Redis service isnot available. This is useful at startup while the application comesonline, but also makes the application more resilient if the Redisservice needs to be restarted anytime during the app's lifetime. In acluster, this also helps handling momentary connection drops betweennodes.
Create another file called requirements.txt in your project directory andpaste the following code in:
flaskredisCreate a Dockerfile and paste the following code in:
# syntax=docker/dockerfile:1FROM python:3.10-alpineWORKDIR /codeENV FLASK_APP=app.pyENV FLASK_RUN_HOST=0.0.0.0RUN apk add --no-cache gcc musl-dev linux-headersCOPY requirements.txt requirements.txtRUN pip install -r requirements.txtEXPOSE 5000COPY . .CMD ["flask", "run", "--debug"]This tells Docker to:
Build an image starting with the Python 3.10 image.Set the working directory to /code.Set environment variables used by the flask command.Install gcc and other dependenciesCopy requirements.txt and install the Python dependencies.Add metadata to the image to describe that the container is listening on port 5000Copy the current directory . in the project to the workdir . in the image.Set the default command for the container to flask run --debug.Important
Check that the Dockerfile has no file extension like .txt. Some editors may append this file extension automatically which results in an error when you run the application.
For more information on how to write Dockerfiles, see theDockerfile reference.
Step 2: Define services in a Compose fileCompose simplifies the control of your entire application stack, making it easy to manage services, networks, and volumes in a single, comprehensible YAML configuration file.
Create a file called compose.yaml in your project directory and pastethe following:
services: web:build: .ports: - "8000:5000" redis:image: "redis:alpine"This Compose file defines two services: web and redis.
The web service uses an image that's built from the Dockerfile in the current directory.It then binds the container and the host machine to the exposed port, 8000. This example service uses the default port for the Flask web server, 5000.
The redis service uses a publicRedisimage pulled from the Docker Hub registry.
For more information on the compose.yaml file, seeHow Compose works.
Step 3: Build and run your app with ComposeWith a single command, you create and start all the services from your configuration file.
From your project directory, start up your application by running docker compose up.
$ docker compose upCreating network "composetest_default" with the default driverCreating composetest_web_1 ...Creating composetest_redis_1 ...Creating composetest_web_1Creating composetest_redis_1 ... doneAttaching to composetest_web_1, composetest_redis_1web_1| * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)redis_1 | 1:C 17 Aug 22:11:10.480 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Ooredis_1 | 1:C 17 Aug 22:11:10.480 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=1, just startedredis_1 | 1:C 17 Aug 22:11:10.480 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.confweb_1| * Restarting with statredis_1 | 1:M 17 Aug 22:11:10.483 * Running mode=standalone, port=6379.redis_1 | 1:M 17 Aug 22:11:10.483 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.web_1| * Debugger is active!redis_1 | 1:M 17 Aug 22:11:10.483 # Server initializedredis_1 | 1:M 17 Aug 22:11:10.483 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.web_1| * Debugger PIN: 330-787-903redis_1 | 1:M 17 Aug 22:11:10.483 * Ready to accept connectionsCompose pulls a Redis image, builds an image for your code, and starts theservices you defined. In this case, the code is statically copied into the image at build time.
Enter http://localhost:8000/ in a browser to see the application running.
If this doesn't resolve, you can also try http://127.0.0.1:8000.
You should see a message in your browser saying:
Hello World! I have been seen 1 times.Refresh the page.
The number should increment.
Hello World! I have been seen 2 times.Switch to another terminal window, and type docker image ls to list local images.
Listing images at this point should return redis and web.
$ docker image lsREPOSITORYTAGIMAGE ID CREATEDSIZEcomposetest_weblateste2c21aa48cc1 4 minutes ago 93.8MBpython3.4-alpine84e6077c7ab6 7 days ago 82.5MBredis alpine9d8fa9aa0e5b 3 weeks ago27.5MBYou can inspect images with docker inspect .
Stop the application, either by running docker compose downfrom within your project directory in the second terminal, or byhitting CTRL+C in the original terminal where you started the app.
Step 4: Edit the Compose file to use Compose WatchEdit the compose.yaml file in your project directory to use watch so you can preview your running Compose services which are automatically updated as you edit and save your code:
services: web:build: .ports: - "8000:5000"develop: watch:- action: sync path: . target: /code redis:image: "redis:alpine"Whenever a file is changed, Compose syncs the file to the corresponding location under /code inside the container. Once copied, the bundler updates the running application without a restart.
For more information on how Compose Watch works, seeUse Compose Watch. Alternatively, seeManage data in containers for other options.
Note
For this example to work, the --debug option is added to the Dockerfile. The --debug option in Flask enables automatic code reload, making it possible to work on the backend API without the need to restart or rebuild the container.After changing the .py file, subsequent API calls will use the new code, but the browser UI will not automatically refresh in this small example. Most frontend development servers include native live reload support that works with Compose.
Step 5: Re-build and run the app with ComposeFrom your project directory, type docker compose watch or docker compose up --watch to build and launch the app and start the file watch mode.
$ docker compose watch[+] Running 2/2 ✔ Container docs-redis-1 Created0.0s ✔ Container docs-web-1Recreated 0.1sAttaching to redis-1, web-1 ⦿ watch enabled...Check the Hello World message in a web browser again, and refresh to see thecount increment.
Step 6: Update the applicationTo see Compose Watch in action:
Change the greeting in app.py and save it. For example, change the Hello World!message to Hello from Docker!:
return 'Hello from Docker! I have been seen {} times.\n'.format(count)Refresh the app in your browser. The greeting should be updated, and thecounter should still be incrementing.
Once you're done, run docker compose down.
Step 7: Split up your servicesUsing multiple Compose files lets you customize a Compose application for different environments or workflows. This is useful for large applications that may use dozens of containers, with ownership distributed across multiple teams.
In your project folder, create a new Compose file called infra.yaml.
Cut the Redis service from your compose.yaml file and paste it into your new infra.yaml file. Make sure you add the services top-level attribute at the top of your file. Your infra.yaml file should now look like this:
services: redis:image: "redis:alpine"In your compose.yaml file, add the include top-level attribute along with the path to the infra.yaml file.
include:- infra.yamlservices: web:build: .ports: - "8000:5000"develop: watch:- action: sync path: . target: /codeRun docker compose up to build the app with the updated Compose files, and run it. You should see the Hello world message in your browser.
This is a simplified example, but it demonstrates the basic principle of include and how it can make it easier to modularize complex applications into sub-Compose files. For more information on include and working with multiple Compose files, seeWorking with multiple Compose files.
Step 8: Experiment with some other commandsIf you want to run your services in the background, you can pass the -d flag (for "detached" mode) to docker compose up and use docker compose ps to see what is currently running:
$ docker compose up -dStarting composetest_redis_1...Starting composetest_web_1...$ docker compose psName CommandStatePorts -------------------------------------------------------------------------------------composetest_redis_1docker-entrypoint.sh redis ...Up 6379/tcp composetest_web_1 flask runUp 0.0.0.0:8000->5000/tcpRun docker compose --help to see other available commands.
If you started Compose with docker compose up -d, stop your services once you've finished with them:
$ docker compose stopYou can bring everything down, removing the containers entirely, with the docker compose down command.
Where to go nextTry theSample apps with ComposeExplore the full list of Compose commandsExplore the Compose file referenceCheck out the Learning Docker Compose video on LinkedIn Learning